package com.hst.capacity.facade.rest;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.NotificationBiz;
import com.hst.capacity.application.biz.OrderBiz;
import com.hst.capacity.application.biz.OrderPoolBiz;
import com.hst.capacity.application.biz.TokenBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.auth.TokenEntity;
import com.hst.capacity.domain.model.request.OrderForDispatchParam;
import com.hst.capacity.domain.model.request.OrderParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchPartyDriverParam;
import com.hst.capacity.domain.model.request.dispatcher.DispatcherOrderParam;
import com.hst.capacity.domain.model.request.export.OrderExportParam;
import com.hst.capacity.domain.model.request.express.ExListNewParam;
import com.hst.capacity.domain.model.request.specialcar.SpListParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.service.DriverService;
import com.hst.capacity.domain.service.TokenService;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  订单共通接口
 *
 * */
@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    OrderBiz orderBiz;

    @Autowired
    OrderPoolBiz orderPoolBiz;

    @Autowired
    DriverService driverService;

    @Autowired
    TokenBiz tokenBiz;

    @Autowired
    NotificationBiz notificationBiz;

    /**
     * 刷新订单
     * @param param
     * @return
     */
    @PostMapping("/refresh")
    private DefaultVO refresh(@RequestBody @Valid OrderParam param){
        return success(orderBiz.refresh(param));
    }

    /**
     * 更换司机
     * @param param
     * @return
     */
    @PostMapping("/redispatch/driver")
    private DefaultVO changeDriver(@RequestBody @Valid DispatchDriverParam param){
        return success(orderBiz.changeDriver(param));
    }

    /**
     * 派单
     * @param param
     * @return
     */
    @PostMapping("/dispatch/driver")
    private DefaultVO dispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        return success(orderBiz.dispatchDriver(param));
    }

    /**
     * 司机外调
     * @param param
     * @return
     */
    @PostMapping("/dispatch/driver/party")
    private DefaultVO dispatchPartyDriver(@RequestBody @Valid DispatchPartyDriverParam param){
        // todo 未实装
        return success(orderBiz.dispatchPartyDriver(param));
    }

    /**
     * 虚拟接单
     * @param param
     * @return
     */
    @PostMapping("/virtual/accept")
    private DefaultVO virtualDispatchDriver(@RequestBody @Valid DispatchParam param){
        // todo 虚拟接单
        return success(orderBiz.virtualAcceptOrder(param));
    }

    /**
     * 派单时订单详细信息
     * @param param
     * @return
     */
    @PostMapping("/dispatch/info/order")
    private DefaultVO getOrderDetail(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.getOrderDetail(idOrder));
    }

    /**
     * 派单时订单操作详细信息
     * @param param
     * @return
     */
    @PostMapping("/dispatch/info/log")
    private DefaultVO getOrderOperationLogList(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.getOrderOperationLogList(idOrder));
    }

    /**
     * 司机比例获得
     * @param param
     * @return
     */
    @PostMapping("/driver/rate")
    private DefaultVO getDriverRate(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        String moduleType = param.get("moduleType");
        return success(driverService.driverRate(idFranchisee, Integer.parseInt(moduleType)));
    }

    /**
     * 司机所得更新
     * @param param
     * @return
     */
    @PostMapping("/driver/change/income")
    private DefaultVO changeDriverIncome(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        String driverIncome = param.get("driverIncome");
        return success(orderBiz.changeDriverIncome(idOrder, Float.valueOf(driverIncome)));
    }

    /**
     * 完单
     * @param param
     * @return
     */
    @PostMapping("/finish")
    private DefaultVO finishOrder(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.finishOrder(idOrder));
    }

    /**
     * 取消订单
     * @param param
     * @return
     */
    @PostMapping("/cancel")
    private DefaultVO cancelOrder(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.cancelOrder(idOrder,param.get("dispatcher")));
    }

    /**
     * 全部未派单订单
     * @param param
     * @return
     */
    @PostMapping("/dispatch/list")
    private DefaultVO getOrderDispatchList(@RequestBody @Valid OrderForDispatchParam param, HttpServletRequest request){
        param.setIdDispatcher(tokenBiz.findIdUser(request));
        DefaultVO vo = success(orderBiz.getOrderDispatchList(param));
        log.info("全部未派单订单数据获取成功");
        return vo;
    }

    /**
     * 未处理虚拟单校验
     * @param param
     * @return
     */
    @PostMapping("/dispatch/cnt")
    private DefaultVO getUnDispatchVirtualOrder(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"服务商ID-必填[idFranchisee]不能为空");
        return success(orderBiz.findUnDispatchVirtualOrder(idFranchisee));
    }

    /**
     * 订单检索查询
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO search(@RequestBody @Valid SpListParam param, HttpServletRequest request){
        boolean historyFlg = false;
        if (StrUtil.isEmpty(param.getHistoryKbn()))
            historyFlg = true;
        param.setIdDispatcher(tokenBiz.findIdUser(request));
        return success(orderPoolBiz.orderSearch(param,historyFlg));
    }

    /**
     * 下载检索
     * @param param
     * @return
     */
    @PostMapping("/export/list")
    private DefaultVO getExportOrderListData(@RequestBody @Valid OrderExportParam param, HttpServletRequest request) {
        log.info("OrderExportParam :{}", JSON.toJSONString(param));
        if (param.getModuleType() == null || param.getHistoryFlg() == null)
            throw new BusinessException(4001, "业务类型和入账参数是必须输入项。");
        param.setIdUser(tokenBiz.findIdUser(request));
        return success(orderBiz.findExportOrderPageListData(param));
    }

}
